home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / sys / RISC_OS / c / arcmkdrawf < prev   
Encoding:
Text File  |  1996-04-13  |  13.3 KB  |  488 lines

  1. /* ///////////////////////////////////////////////////////////////////
  2. // $Id: arcmkdrawf.c,v 1.0 1995/08/29 06:03:13  Exp $
  3.  * Revision 1.0 1.0 1995/08/29 06:03:13
  4.  * Acorn RISC OS draw writer, contributed by Maurizio Ferrari
  5.  * (ferrari@bologna.marelli.it).
  6.  *
  7. //
  8. //  File:       arcmkdrawf.c
  9. //
  10. //  Descript:   RISC OS draw drivers
  11. //
  12. //  Library:    ---
  13. //
  14. //  Requires:   ---
  15. //
  16. //  Public:     plD_init_arcmkdrawf()
  17. //              plD_line_arcmkdrawf()
  18. //              plD_polyline_arcmkdrawf()
  19. //              plD_eop_arcmkdrawf()
  20. //              plD_bop_arcmkdrawf()
  21. //              plD_tidy_arcmkdrawf()
  22. //              plD_state_arcmkdrawf()
  23. //              plD_esc_arcmkdrawf()
  24. //
  25. //              pldummy_arcmkdrawf()
  26. //
  27. //  Private:    arcmkdrawf_initialize_pls()
  28. //
  29. //  Notes:      ---
  30. //
  31. //  Revisions:
  32. /////////////////////////////////////////////////////////////////// */
  33.  
  34. #include "plDevs.h"
  35.  
  36. #if defined(PLD_arcmkdrawf)
  37.  
  38. #include "plplotP.h"
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "drivers.h"
  42.  
  43.  
  44. /* top level declarations */
  45.  
  46. /* Page sizes */
  47.  
  48. #define ARCDRAW_XMIN  25
  49. #define ARCDRAW_XMAX  800
  50. #define ARCDRAW_YMIN  30
  51. #define ARCDRAW_YMAX  580
  52. #define ARCDRAW_XRATIO 4.
  53. #define ARCDRAW_YRATIO 4.
  54.  
  55.  
  56. static struct lvs {
  57. int path_is_open;
  58. int width_has_changed;
  59. int color_0_has_changed;
  60. int color_1_has_changed;
  61. int x1st;
  62. int y1st;
  63. int xlast;
  64. int ylast;
  65. int fill_req;
  66. int must_close;
  67. int path_is_valid;
  68. PLColor fill_color;
  69. } local_v;
  70.  
  71. /*local prototypes */
  72. static void arcmkdrawf_end_path(PLStream *, struct lvs *);
  73. static void arcmkdrawf_start_path(PLStream *, struct lvs *);
  74. static void arcmkdrawf_initialize_pls(PLStream *, struct lvs *);
  75. static void arcmkdrawf_change_colour0(PLStream *, struct lvs *);
  76. static void arcmkdrawf_change_colour1(PLStream *, struct lvs *);
  77. static void arcmkdrawf_change_width(PLStream *, struct lvs *);
  78.  
  79. /*----------------------------------------------------------------------*\
  80.  * arcmkdrawf_initialize_pls()
  81.  *
  82.  * Initialize plot stream
  83. \*----------------------------------------------------------------------*/
  84.  
  85. static void
  86. arcmkdrawf_initialize_pls(PLStream *pls, struct lvs *l_v)
  87. {
  88.  
  89.     PLDev *dev = (PLDev *) pls->dev;
  90.  
  91.     /*clear local work variables*/
  92.     l_v->path_is_open = FALSE;
  93.     l_v->width_has_changed = FALSE;
  94.     l_v->color_0_has_changed = FALSE;
  95.     l_v->color_1_has_changed = FALSE;
  96.     l_v->x1st = 0;
  97.     l_v->y1st = 0;
  98.     l_v->xlast = 0;
  99.     l_v->ylast = 0;
  100.     l_v->fill_req = FALSE;
  101.     l_v->must_close = FALSE;
  102.     l_v->path_is_valid = FALSE;
  103.  
  104.     pls->termin = 0;            /* not an interactive terminal */
  105.     pls->icol1 = 1;
  106.     pls->bytecnt = 0;
  107.     pls->page = 0;
  108.     pls->family = 1;            /* file has family */
  109.     pls->dev_fill0 = 1;        /* Can do solid fills */
  110.  
  111.     plFamInit(pls);             /* Initialize family file info */
  112.     plOpenFile(pls);            /* get file name if not already set */
  113.  
  114.     dev->xold = UNDEFINED;
  115.     dev->yold = UNDEFINED;
  116.     dev->xlen = dev->xmax - dev->xmin;
  117.     dev->ylen = dev->ymax - dev->ymin;
  118.  
  119.     plP_setpxl((PLFLT) ARCDRAW_XRATIO, (PLFLT) ARCDRAW_YRATIO);
  120.     plP_setphy(dev->xmin, dev->xmax, dev->ymin, dev->ymax);
  121.  
  122. }
  123.  
  124. /*----------------------------------------------------------------------*\
  125.  * plD_init_arcmkdrawf()
  126.  *
  127.  * Initialize device.
  128. \*----------------------------------------------------------------------*/
  129.  
  130. void
  131. plD_init_arcmkdrawf(PLStream *pls)
  132. {
  133.     PLDev *dev;
  134.  
  135.     pls->color = 1;
  136.     dev = plAllocDev(pls);      /* Allocate device-specific data */
  137.     dev->xmin = ARCDRAW_XMIN;
  138.     dev->xmax = ARCDRAW_XMAX;
  139.     dev->ymin = ARCDRAW_YMIN;
  140.     dev->ymax = ARCDRAW_YMAX;
  141.  
  142.     arcmkdrawf_initialize_pls(pls, &local_v);   /* initialize plot stream */
  143. }
  144.  
  145. /*----------------------------------------------------------------------*\
  146.  * plD_line_arcmkdrawf()
  147.  *
  148.  * Draw a line in the current color from (x1,y1) to (x2,y2).
  149. \*----------------------------------------------------------------------*/
  150.  
  151. void
  152. plD_line_arcmkdrawf(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
  153. {
  154.     PLDev *dev = (PLDev *) pls->dev;
  155.     int x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
  156.  
  157.    /* Write out old path */
  158.     if (x1 != dev->xold || y1 != dev->yold) {    /* Write out old path */
  159.       if (local_v.path_is_open != FALSE) {
  160.         arcmkdrawf_end_path(pls, &local_v);
  161.       }
  162.       local_v.path_is_open = TRUE;
  163.       fputs( "#Line\n", pls->OutFile );
  164.       arcmkdrawf_start_path(pls, &local_v);
  165.       pls->bytecnt += fprintf( pls->OutFile, "Move %d %d\n", x1, y1 );
  166.       local_v.x1st = x1; local_v.y1st = y1;
  167.       local_v.color_0_has_changed = FALSE;
  168.       local_v.color_1_has_changed = FALSE;
  169.     }
  170.     /* Add new point to path */
  171.     if (local_v.width_has_changed == TRUE){
  172.       arcmkdrawf_change_width(pls, &local_v);
  173.     }
  174.     if (local_v.color_0_has_changed == TRUE) {
  175.       arcmkdrawf_change_colour0(pls, &local_v);
  176.     }
  177.     if (local_v.color_1_has_changed == TRUE) {
  178.       arcmkdrawf_change_colour1(pls, &local_v);
  179.     }
  180.     pls->bytecnt += fprintf( pls->OutFile, "Line %d %d\n", x2, y2);
  181.     local_v.xlast = x2; local_v.ylast = y2;
  182.     if ((local_v.x1st != local_v.xlast) || (local_v.y1st != local_v.ylast)) {
  183.       local_v.path_is_valid = TRUE;
  184.     }
  185.  
  186.     dev->xold = x2;
  187.     dev->yold = y2;
  188. }
  189.  
  190. /*----------------------------------------------------------------------*\
  191.  * plD_polyline_arcmkdrawf()
  192.  *
  193.  * Draw a polyline in the current color.
  194. \*----------------------------------------------------------------------*/
  195.  
  196. void
  197. plD_polyline_arcmkdrawf(PLStream *pls, short *xa, short *ya, PLINT npts)
  198. {
  199.   register PLINT i;
  200.   PLDev *dev = (PLDev *) pls->dev;
  201.  
  202.  
  203.   /* Write out old path */
  204. /*  if (xa[0] != dev->xold || ya[0] != dev->yold) {*/
  205.     if (local_v.path_is_open != FALSE) {
  206.       arcmkdrawf_end_path(pls, &local_v);
  207.     }
  208.     fputs( "#Polyline\n", pls->OutFile );
  209.     arcmkdrawf_start_path(pls, &local_v);
  210.     pls->bytecnt += fprintf( pls->OutFile, "Move %d %d\n", xa[0], ya[0] );
  211.     local_v.x1st = xa[0]; local_v.y1st = ya[0];
  212. /*  }*/
  213.   for (i = 1; i < npts; i++) {        /* Add new point to path */
  214.     pls->bytecnt += fprintf( pls->OutFile, "Line %d %d\n", xa[i], ya[i] );
  215.     local_v.xlast = xa[i]; local_v.ylast = ya[i] ;
  216.     if ((local_v.x1st != local_v.xlast) || (local_v.y1st != local_v.ylast)) {
  217.       local_v.path_is_valid = TRUE;
  218.     }
  219.   }
  220.   arcmkdrawf_end_path(pls, &local_v);
  221.   dev->xold = xa[ npts - 1 ];
  222.   dev->yold = ya[ npts - 1 ];
  223. }
  224.  
  225. /*----------------------------------------------------------------------*\
  226.  * plD_eop_arcmkdrawf()
  227.  *
  228.  * End of page.
  229. \*----------------------------------------------------------------------*/
  230.  
  231. void
  232. plD_eop_arcmkdrawf(PLStream *pls)
  233. {
  234. #ifdef __riscos
  235.     char *sys_string;
  236. #endif
  237.     if (local_v.path_is_open != FALSE) {
  238.       arcmkdrawf_end_path(pls, &local_v);
  239.     }
  240.     fclose(pls->OutFile);
  241. #ifdef __riscos
  242.     sys_string=malloc(sizeof(pls->FileName)+15);
  243.     if (sys_string == NULL) {
  244.       plwarn("malloc failed - cannot change filetype to TXT");
  245.     }
  246.     else {
  247.       strcpy(sys_string, "settype ");
  248.       strcat(sys_string, pls->FileName);
  249.       strcat(sys_string, " FFF");
  250. /*      printf("system string is %s", sys_string);*/
  251.       system(sys_string);
  252.       free(sys_string);
  253.     }
  254. #endif
  255. }
  256.  
  257. /*----------------------------------------------------------------------*\
  258.  * plD_bop_arcmkdrawf()
  259.  *
  260.  * Set up for the next page.
  261.  * Advance to next family file if necessary (file output).
  262. \*----------------------------------------------------------------------*/
  263.  
  264. void
  265. plD_bop_arcmkdrawf(PLStream *pls)
  266. {
  267.     PLDev *dev = (PLDev *) pls->dev;
  268.  
  269.     dev->xold = UNDEFINED;
  270.     dev->yold = UNDEFINED;
  271.  
  272.     if (!pls->termin){
  273.         plGetFam(pls);
  274.     }
  275.     pls->famadv = 1; /*advance to next member*/
  276.     pls->page++;
  277.     fputs( "#Created by plplot driver\n", pls->OutFile );
  278.     fputs( "Options {\n", pls->OutFile );
  279.     fputs( "  PaperSize 4     # A4\n", pls->OutFile );
  280.     fputs( "  Limits {\n", pls->OutFile );
  281.     fputs( "    Landscape\n", pls->OutFile );
  282.     fputs( "  }\n", pls->OutFile );
  283.     fputs( "}\n", pls->OutFile );
  284.     fputs( "Path {\n", pls->OutFile );
  285.     fputs( "FillColour r0g0b0\n", pls->OutFile );
  286.     fputs( "OutlineColour r0g0b0\n", pls->OutFile );
  287.     fputs( "Width 0\n", pls->OutFile );
  288.     fputs( "Move 0 594\n", pls->OutFile );
  289.     fputs( "Line 840 594\n", pls->OutFile );
  290.     fputs( "Line 840 0\n", pls->OutFile );
  291.     fputs( "Line 0 0\n", pls->OutFile );
  292.     fputs( "Line 0 594\n", pls->OutFile );
  293.     fputs( "Close\n", pls->OutFile );
  294.     fputs( "}\n", pls->OutFile );
  295. }
  296.  
  297. /*----------------------------------------------------------------------*\
  298.  * plD_tidy_arcmkdrawf()
  299.  *
  300.  * Close graphics file or otherwise clean up.
  301. \*----------------------------------------------------------------------*/
  302.  
  303. void
  304. plD_tidy_arcmkdrawf(PLStream *pls)
  305. {
  306. #ifdef __riscos
  307.     char *sys_string;
  308. #endif
  309.     if (local_v.path_is_open != FALSE) {
  310.       arcmkdrawf_end_path(pls, &local_v);
  311.     }
  312.     fclose(pls->OutFile);
  313. #ifdef __riscos
  314.     sys_string=malloc(sizeof(pls->FileName)+15);
  315.     if (sys_string == NULL) {
  316.       plwarn("malloc failed - cannot change filetype to TXT");
  317.     }
  318.     else {
  319.       strcpy(sys_string, "settype ");
  320.       strcat(sys_string, pls->FileName);
  321.       strcat(sys_string, " FFF");
  322.       system(sys_string);
  323.       free(sys_string);
  324.     }
  325. #endif
  326.  
  327. }
  328.  
  329. /*----------------------------------------------------------------------*\
  330.  * plD_state_arcmkdrawf()
  331.  *
  332.  * Handle change in PLStream state (color, pen width, fill attribute, etc).
  333. \*----------------------------------------------------------------------*/
  334.  
  335. void
  336. plD_state_arcmkdrawf(PLStream *pls, PLINT op)
  337. {
  338.     switch (op) {
  339.  
  340.     case PLSTATE_WIDTH:
  341.         local_v.width_has_changed = TRUE;
  342.         break;
  343.  
  344.     case PLSTATE_COLOR0:
  345.         local_v.color_0_has_changed = TRUE;
  346.         break;
  347.  
  348.     case PLSTATE_COLOR1:
  349.         local_v.color_1_has_changed = TRUE;
  350.         break;
  351.  
  352.     case PLSTATE_FILL:
  353.         fputs( "#PLSTATE_FILL - ignored\n", pls->OutFile );
  354.         break;
  355.  
  356.     default:
  357.         fprintf( pls->OutFile, "#PLSTATE_DEFAULT - code = %d ignored\n", op );
  358.         break;
  359.     }
  360. }
  361.  
  362. /*----------------------------------------------------------------------*\
  363.  * plD_esc_arcmkdrawf()
  364.  *
  365.  * Escape function.
  366. \*----------------------------------------------------------------------*/
  367.  
  368. void
  369. plD_esc_arcmkdrawf(PLStream *pls, PLINT op, void *ptr)
  370. {
  371.     switch (op) {
  372.  
  373.       case PLESC_FILL:
  374.         local_v.fill_req = TRUE;
  375.         local_v.fill_color = pls->curcolor;
  376.         plD_polyline_arcmkdrawf(pls, pls->dev_x, pls->dev_y, pls->dev_npts);
  377.     break;
  378.  
  379.       default:
  380.         fprintf( pls->OutFile, "#PLESC_DEFAULT - code = %d ignored\n", op );
  381.         break;
  382.     }
  383.  
  384. }
  385.  
  386. /*----------------------------------------------------------------------*\
  387.  * arcmkdrawf_start_path()
  388.  *
  389.  * start a new path function.
  390. \*----------------------------------------------------------------------*/
  391.  
  392. static void
  393. arcmkdrawf_start_path(PLStream *pls, struct lvs *l_v)
  394. {
  395.     l_v->path_is_open = TRUE;
  396.     l_v->path_is_valid = FALSE;
  397.     fputs( "Path {\n", pls->OutFile );
  398.     if ( pls->width == 0 ) {/*i.e. not initialised*/
  399.       fputs( "Width 0.5\n", pls->OutFile );
  400.     } else {
  401.       fprintf( pls->OutFile, "Width %d\n", pls->width);
  402.     }
  403.     fprintf( pls->OutFile, "OutlineColour r%dg%db%d\n", pls->curcolor.r, pls->curcolor.g, pls->curcolor.b);
  404.     if (l_v->fill_req == TRUE) {
  405.       l_v->fill_req = FALSE;
  406.       l_v->must_close = TRUE;
  407.     }
  408.       l_v->color_0_has_changed = FALSE;
  409.       l_v->color_1_has_changed = FALSE;
  410.  
  411. }
  412.  
  413. /*----------------------------------------------------------------------*\
  414.  * arcmkdrawf_start_path()
  415.  *
  416.  * start a new path function.
  417. \*----------------------------------------------------------------------*/
  418.  
  419. static void
  420. arcmkdrawf_end_path(PLStream *pls, struct lvs *l_v)
  421. {
  422.     /* this should patch rogue draw paths */
  423.     if (l_v->path_is_valid == FALSE) {
  424.       fputs( "OutlineColour Transparent\nWidth 0\n", pls->OutFile );
  425.       fprintf( pls->OutFile, "Move %d %d\n", l_v->xlast+1, l_v->ylast+1);
  426.     }
  427.     if (l_v->must_close == TRUE) {
  428.       fprintf( pls->OutFile, "FillColour r%dg%db%d\n", l_v->fill_color.r, l_v->fill_color.g, l_v->fill_color.b);
  429.       fputs( "close\n", pls->OutFile );
  430.       l_v->must_close = FALSE;
  431.     }
  432.     fputs( "}\n", pls->OutFile );
  433.     l_v->path_is_open = FALSE;
  434. }
  435.  
  436.  
  437. /*----------------------------------------------------------------------*\
  438.  * arcmkdrawf_change_colour0()
  439.  *
  440.  * change colour0 function.
  441. \*----------------------------------------------------------------------*/
  442.  
  443. static void
  444. arcmkdrawf_change_colour0(PLStream *pls, struct lvs *l_v)
  445. {
  446.       fprintf( pls->OutFile, "OutlineColour r%dg%db%d\n", pls->curcolor.r, pls->curcolor.g, pls->curcolor.b);
  447.       l_v->color_0_has_changed = FALSE;
  448.  
  449. }
  450.  
  451. /*----------------------------------------------------------------------*\
  452.  * arcmkdrawf_change_colour1()
  453.  *
  454.  * change colour1 function.
  455. \*----------------------------------------------------------------------*/
  456.  
  457. static void
  458. arcmkdrawf_change_colour1(PLStream *pls, struct lvs *l_v)
  459. {
  460.       fprintf( pls->OutFile, "OutlineColour r%dg%db%d\n", pls->curcolor.r, pls->curcolor.g, pls->curcolor.b);
  461.       l_v->color_1_has_changed = FALSE;
  462.  
  463. }
  464.  
  465. /*----------------------------------------------------------------------*\
  466.  * arcmkdrawf_change_width()
  467.  *
  468.  * change colour1 function.
  469. \*----------------------------------------------------------------------*/
  470.  
  471. static void
  472. arcmkdrawf_change_width(PLStream *pls, struct lvs *l_v)
  473. {
  474.       l_v->width_has_changed = FALSE;
  475.       fprintf( pls->OutFile, "Width %d\n", pls->width);
  476. }
  477.  
  478.  
  479. #else
  480. int
  481. pldummy_arcmkdrawf(void)
  482. {
  483.     return 0;
  484. }
  485.  
  486. #endif        /* PLD_arcmkdrawf */
  487.  
  488.